package com.csw.android.ffmpegtest.ui.main

import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.csw.android.dev_utils.ui.CommonActivity
import com.csw.android.ffmpegtest.BR
import com.csw.android.ffmpegtest.R
import com.csw.android.ffmpegtest.databinding.ItemMainSectionBinding
import com.csw.android.ffmpegtest.databinding.ItemMainSectionSecondBinding
import com.csw.android.ffmpegtest.entity.MainSectionItem
import com.csw.android.ffmpegtest.entity.MainSectionSecondItem

class SectionAdapter :
    BaseNodeAdapter() {
    init {
        setOnItemClickListener { adapter, view, position ->
            when (adapter.getItemViewType(position)) {
                //开关子列表
                MainSectionItemProvider.VIEW_TYPE -> {
                    adapter.run {
                        expandOrCollapse(position)
                    }
                }
                //跳转界面
                MainSectionSecondItemProvider.VIEW_TYPE -> {
                    val item = adapter.getItem(position) as MainSectionSecondItem
                    CommonActivity.openActivity(view.context, item.clazz as Class<Fragment>, null)
                }
            }
        }
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (data[position]) {
            is MainSectionItem -> MainSectionItemProvider.VIEW_TYPE
            is MainSectionSecondItem -> MainSectionSecondItemProvider.VIEW_TYPE
            else -> {
                throw IllegalArgumentException("unknown type ${data[position]}")
            }
        }
    }

    init {
        addFullSpanNodeProvider(MainSectionItemProvider())
        addItemProvider(MainSectionSecondItemProvider())
    }

    override fun getItemProvider(viewType: Int): BaseItemProvider<BaseNode>? {
        when (viewType) {
            MainSectionItemProvider.VIEW_TYPE -> MainSectionItemProvider()
            MainSectionSecondItemProvider.VIEW_TYPE -> MainSectionSecondItemProvider()
        }
        return super.getItemProvider(viewType)
    }
}

class MainSectionItemProvider : BaseNodeProvider() {
    companion object {
        const val VIEW_TYPE = 0
    }

    override val itemViewType: Int
        get() = VIEW_TYPE
    override val layoutId: Int
        get() = R.layout.item_main_section

    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        DataBindingUtil.bind<ItemMainSectionBinding>(helper.itemView)?.run {
            setVariable(BR.item, item)
        }
    }
}

class MainSectionSecondItemProvider : BaseNodeProvider() {
    companion object {
        const val VIEW_TYPE = 1
    }

    override val itemViewType: Int
        get() = VIEW_TYPE
    override val layoutId: Int
        get() = R.layout.item_main_section_second

    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        DataBindingUtil.bind<ItemMainSectionSecondBinding>(helper.itemView)?.run {
            setVariable(BR.item, item)
        }
    }
}